<template>
  <!--  退单管理页面  -->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <search :config="searchConfig" @search="search" @getHeight="getHeight"/>

    <!-- table区域-begin -->
    <div class="page_table">
      <vxe-grid
        ref="xGrid"
        :border="true"
        :resizable="true"
        :showOverflow="true"
        size="mini"
        :loading="loading"
        :height="pageHeight-seachHeight-94"
        :align="left"
        :columns="columns"
        :data="dataSource"
        :toolbar-config="tableToolbar"
        :checkbox-config="checkboxConfig"
        v-bind="vexConfig"
        @checkbox-change="selectChangeEvent"
        @checkbox-all="selectChangeEvent"
      >
        <template #toolbar_buttons>
          <!-- 操作按钮区域 -->
          <div class="table-operator">
            <a-button @click="handleAdd" type="primary" v-has="'ordermgt:add'">新增</a-button>
            <a-button v-has="'ordermgt:commit'" type="primary" @click="submit">提交</a-button>
            <a-button v-has="'ordermgt:delete'" @click="batchDel">删除</a-button>
            <a-button v-has="'ordermgt:recall'" @click="withdraw">撤回</a-button>
            <a-button v-has="'ordermgt:export'" @click="downExportXls('chargeback_mag' )">导出</a-button>
          </div>
        </template>
        <template #detail="{ row,rowIndex }">
          <a @click="handleDetail(row)">{{ row.backCode }}</a>
        </template>
        <template #chargebackType="{ row,rowIndex }">
          {{ filterDictText(omsList, row.chargebackType) }}
        </template>

        <template #status="{ row,rowIndex }">
          {{ filterDictText(backList, row.status) }}
        </template>
        <template #action="{ row }">
          <a v-has="'ordermgt:update'" v-if="[0,1,3].includes(row.status)" @click="handleEdit(row)">编辑</a>
          <a-divider type="vertical"/>
          <a v-has="'business:log'" @click='handleBusiness("退单管理",row)'>日志</a>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync="ipagination.current"
            :page-size.sync="ipagination.pageSize"
            :total="ipagination.total"
            :pageSizes="ipagination.pageSizeOptions"
            @page-change="handlePageChange">
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!--    新增弹窗组件  -->
    <chargeBackmgtModal ref="modalForm" @ok="modalFormOk"></chargeBackmgtModal>
    <business-log ref="businessLog"></business-log>
  </a-card>
</template>

<script>
import { PageListMixin } from '@/mixins/PageListMixin'
import { deleteAction, getAction, postAction } from '@api/manage'
//新增弹窗组件
import chargeBackmgtModal from './TerminalOrderModal/chargeBackmgtModal'
import Vue from 'vue'
import { filterDictText,initDictOptions } from '@comp/dict/CnDictSelectUtil'

export default {
  name: 'userorgerl',
  mixins: [PageListMixin],
  components: {
    chargeBackmgtModal, //新增弹窗组件
  },
  data() {
    return {
      selectedRowKeys: [],
      description: '用户组织关系',
      searchConfig: [
        {
          type: 'input',
          name: 'backCode',
          placeholder: '退货单号'
        },
        {
          type: 'input',
          name: 'customerCode',
          placeholder: '客户编号'
        },
        {
          type: 'input',
          name: 'customerName',
          placeholder: '客户名称'
        },
        {
          type: 'j-select',
          name: 'status',
          placeholder: '请选择订单状态',
          dictCode: 'back_order_status',
        },

      ],
      columns: [
        { type: 'checkbox', width: 60, fixed: 'left' },
        {
          title: '退货单号',
          align: 'left',
          field: 'backCode',
          minWidth: 80,
          slots: {
            default: 'detail'
          }
        },
        {
          title: 'oms订单编码',
          align: 'left',
          field: 'omsOrderCode',
          minWidth: 120,
        },
        {
          title: '业务类型',
          align: 'left',
          field: 'businessType',
          minWidth: 80,

        },
        {
          title: '客户编号',
          align: 'left',
          field: 'customerCode',
          minWidth: 80,
        },
        {
          title: '客户名称',
          align: 'left',
          field: 'customerName',
          minWidth: 80,
        }
        , {
          title: '所属组织',
          align: 'left',
          field: 'organizationName',
          minWidth: 80,
        },
        {
          title: '退单数量',
          align: 'left',
          field: 'chargebackQuantity',
          minWidth: 80,
        },
        {
          title: '退单金额',
          align: 'left',
          field: 'chargebackAmount',
          minWidth: 80,
        },
        {
          title: '创建人',
          align: 'left',
          minWidth: 80,
          field: 'createName',
        },
        {
          title: '创建时间',
          align: 'left',
          minWidth: 80,
          field: 'createTime',
        },
        {
          title: '更新人',
          align: 'left',
          minWidth: 80,
          field: 'updateName',
        },
        {
          title: '更新时间',
          align: 'left',
          minWidth: 80,
          field: 'updateTime',
        },
        {
          title: '退单状态',
          align: 'left',
          minWidth: 80,
          field: 'status',
          fixed: 'right',
          slots: {
            default: 'status'
          }
        },
        {
          title: '操作',
          field: 'action',
          align: 'center',
          fixed: 'right',
          minWidth: 80,
          slots: {
            default: 'action'
          }
        }
      ],
      omsList:[],
      backList:[],
      url: {
        list: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/findPageList',
        detail: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/getInfo',
        deleteBatch: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/deleteBatch',
        delete: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/delete',
        submit: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/submit',
        withdraw: window._CONFIG['domianDmsURL'] + '/order/dmsOrderBackHead/withdraw',
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
  },
  methods: {
    filterDictText,
    initDictConfig() {
      console.log('--我才是真的方法!--')
      // initDictOptions('oms_business_type').then((res) => {
      //   if (res.success) {
      //     this.omsList = res.result
      //   }
      // })
      initDictOptions('back_order_status').then((res) => {
        if (res.success) {
          this.backList = res.result
        }
      })
    },
    // 撤回
    withdraw: function () {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认撤回',
          content: `已选中${this.selectedRowKeys.length}条,是否撤回选中数据?`,
          onOk: function () {
            that.loading = true
            getAction(that.url.withdraw, { ids: ids }).then((res) => {
              if (res.success) {
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    // 提交
    submit: function () {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认提交',
          content: `已选中${this.selectedRowKeys.length}条,是否提交选中数据?`,
          onOk: function () {
            that.loading = true
            getAction(that.url.submit, { ids: ids }).then((res) => {
              if (res.success) {
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },

    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    changeStatus(e, item) {
      if (e) {
        this.startorstopByid(1, item.id)
      } else {
        this.startorstopByid(0, item.id)
      }
    },
    startorstopByid(status, id) {
      let that = this
      this.$confirm({
        title: '确认修改',
        content: '是否' + (status == 0 ? '停用' : '启动') + '选中数据？',
        onOk() {
          that.loading = true
          getAction(that.url.startOrStopUrl, { ids: id, status: status }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.loading = false
          })
        },
        onCancel() {
          // that.loadData();
          // that.onClearSelected();
        },
      })
    },

  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>
